package org.cainiao.basic.concurrent.juc.collection;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * <br />
 * <p>
 * Author: Cai Niao(wdhlzd@163.com)<br />
 *
 * @param delay 延迟
 */
public record DelayedEvent(long delay) implements Delayed {

    // 单位为毫秒
    private static final TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(delay, TIME_UNIT);
    }

    @Override
    public long delay() {
        return getDelay(TIME_UNIT);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (delay() - o.getDelay(TIME_UNIT));
    }

}
